package com.tdh.tlgusers.service.impl;

import com.tdh.tlgusers.mapper.UserMapper;
import com.tdh.tlgusers.pojo.User;
import com.tdh.tlgusers.service.UserService;
import com.tdh.tlgusers.util.Record;
import com.tdh.tlgusers.util.SecurityUtils;
import com.tdh.tlgusers.util.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Record register(User user) {
        Record record=new Record();
        User user1=null;
        int count=0;
        try {
            user1=userMapper.selByIdOrUserName(user.getUserName(),null);
            if(user1!=null){
                record.setStatus(Status.PHONE_IS_EXISTS);
                record.setMsg("该用户名已存在");
            }else{
                user.setPassword(SecurityUtils.md5Hex1024(user.getPassword()));
                count=userMapper.register(user);
            }

        }catch (Exception e){
            record.setStatus(Status.INSERT_FALIED);
            record.setMsg("注册用户异常！");
            e.printStackTrace();
            return record;
        }

        if (count>0){
            record.setStatus(Status.INSERT_SUCCESS);
            record.setMsg("注册成功");
            record.setData(count);;
        }else{
            record.setStatus(Status.INSERT_FALIED);
            record.setMsg("注册失败。");
        }
        return record;

    }

    @Override
    public Record login(String userName, String password) {
        Record record=new Record();
        User user2=null;
        try {
            user2=userMapper.login(userName,SecurityUtils.md5Hex1024(password));
        }catch (Exception e){
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("用户登陆异常异常！");
            e.printStackTrace();
            return record;
        }

        if (user2!=null){
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("登陆成功。");
            record.setData(user2);
        }else{
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("登陆失败。");
        }
        return record;
    }

//    @Override
//    public Record selUsers() {
//        Record record=new Record();
//        String key="User";
//        ListOperations<String,User> list=redisTemplate.opsForList();
//        List<User> user=null;
//        try {
//            boolean bool=redisTemplate.hasKey(key);
//            if (bool){
//                user=list.range(key,0,-1);
//            }else{
//                user=userMapper.selUsers();
//                list.leftPushAll(key,user);
//            }
//        }catch (Exception e){
//            record.setStatus(Status.SELECT_FALIED);
//            record.setMsg("查询用户信息异常！");
//            e.printStackTrace();
//            return record;
//        }
//
//        if (user!=null && user.size()!=0){
//            record.setStatus(Status.SELECT_SUCCESS);
//            record.setMsg("用户列表查询成功！");
//            record.setData(user);
//        }else{
//            record.setStatus(Status.SELECT_FALIED);
//            record.setMsg("用户列表查询失败！");
//        }
//        return record;
//    }

    @Override
    public Record selUsers() {
        Record record=new Record();
        List<User> user=null;
        try {
            user=userMapper.selUsers();
        }catch (Exception e){
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("查询用户信息异常！");
            e.printStackTrace();
            return record;
        }

        if (user!=null && user.size()!=0){
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("用户列表查询成功！");
            record.setData(user);
        }else{
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("用户列表查询失败！");
        }
        return record;
    }

    @Override
    public Record selByIdOrUserName(String userName,Integer userId) {
        Record record=new Record();
        User user3=null;
        try {
            user3=userMapper.selByIdOrUserName(userName,userId);
        }catch (Exception e){
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("根据用户名或用户id查询异常！");
            e.printStackTrace();
            return record;
        }
        if (user3 != null){
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("根据用户名或用户id查询成功。");
            record.setData(user3);
        }else{
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("根据用户名或用户id查询失败。");
        }
        return record;
    }

    @Override
    public Record modifyDisable(Integer state,Integer userId) {
        Record record=new Record();
        int count=0;
        try {
            count=userMapper.modifyDisable(state,userId);
        }catch (Exception e){
            record.setStatus(Status.UPDATE_FALIED);
            record.setMsg("根据用户id修改用户状态失败！");
            e.printStackTrace();
            return record;
        }

        if (count>0){
            record.setStatus(Status.UPDATE_SUCCESS);
            record.setMsg("修改用户状态成功！");
            record.setData(count);
        }else{
            record.setStatus(Status.UPDATE_FALIED);
            record.setMsg("修改用户状态失败！");
        }
        return record;
}

    @Override
    public Record modifyUserInfo(User user) {
        Record record=new Record();
        int count=0;
        try {
            count=userMapper.modifyUserInfo(user);
        }catch (Exception e){
            record.setStatus(Status.UPDATE_FALIED);
            record.setMsg("修改信息异常");
            e.printStackTrace();
            return record;
        }

        if (count>0){
            record.setStatus(Status.UPDATE_SUCCESS);
            record.setMsg("修改信息成功！");
            record.setData(count);
        }else{
            record.setStatus(Status.UPDATE_FALIED);
            record.setMsg("修改信息失败！");
        }
        return record;
    }
}
